package com.example.zyfypt_713zxy.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyfypt_713zxy.R;
import com.example.zyfypt_713zxy.adapter.ArticleAdapter;
import com.example.zyfypt_713zxy.bean.ArticleBean;
import com.example.zyfypt_713zxy.iface.ArticleListener;
import com.example.zyfypt_713zxy.model.ArticleModel;

import java.util.List;

public class Fragment1 extends BaseFragment {

    private int page = 1;
    private int lastVisibleItemPosition;

    private List<ArticleBean> list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private ArticleAdapter adapter;

    private ArticleListener listener = new ArticleListener() {//
        @Override
        public void onResponse(List<ArticleBean> beanlist) {
            if(page==1)
            {
                list=beanlist;
            }
            else{
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter.setList(list);//
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,"失败"+msg,Toast.LENGTH_SHORT).show();
        }
    };


    public Fragment1(){}

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment1,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--f1--"+getSessionId());//父类

        initRecycleView(view);//

        ArticleModel model = new ArticleModel();//
        model.getResultList(getSessionId(),"article",page,listener);//
    }

    private void initRecycleView(View view) {
        recyclerView = view.findViewById(R.id.recycleview);
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setHasFixedSize(true);
        adapter = new ArticleAdapter(context);
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,int newState){
                super.onScrollStateChanged(recyclerView,newState);
                if(list!=null){
                    if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItemPosition+1==list.size())
                    {
                        page+=1;
//再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                        ArticleModel model=new ArticleModel();
                        model.getResultList(getSessionId(),"article",page,listener);
                    }
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView,int dx,int dy){
                super.onScrolled(recyclerView,dx,dy);
                lastVisibleItemPosition =  layoutManager.findLastVisibleItemPosition();//滚动结束后将赋
            }
        });
    }
}
